package com.ruoyi.leaveStudent.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.UserSchoolInformation;
import com.ruoyi.leaveManage.bo.LeaveManageEditBo;
import com.ruoyi.leaveManage.bo.LeaveManageQueryBo;
import com.ruoyi.leaveManage.domain.LeaveManage;
import com.ruoyi.leaveManage.domain.LeaveUser;
import com.ruoyi.leaveManage.mapper.LeaveManageMapper;
import com.ruoyi.leaveManage.service.ILeaveUserService;
import com.ruoyi.leaveManage.vo.LeaveUserVo;
import com.ruoyi.leaveManage.vo.LeaveVo;
import com.ruoyi.leaveStudent.bo.LeaveAddBo;
import com.ruoyi.leaveStudent.service.ILeaveService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 请假信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-04-26
 */
@Service
public class LeaveServiceImpl extends ServiceImpl<LeaveManageMapper, LeaveManage> implements ILeaveService {

    @Autowired
    private  ISysUserService iSysUserService;


    @Autowired
    private ILeaveUserService iLeaveUserService;


    @Override
    @Transactional
    public Boolean insertByAddBo(LeaveAddBo bo) {
        LeaveManage add = BeanUtil.toBean(bo, LeaveManage.class);
        LeaveUser leaveUserAdd= BeanUtil.toBean(bo, LeaveUser.class);
        Long userIds=iSysUserService.getById(bo.getUserId()).getUserIds();
        SysUser teacher=iSysUserService.getById(userIds);
        String college=iSysUserService.getById(teacher.getUserIds()).getNickName();
        add.setCreateTime(new Date());
        add.setApproveByGuide(teacher.getNickName());
        add.setApproveByLeading(college);
        validEntityBeforeSave(add);
        int i=baseMapper.insert(add);
        if (i<=0){
            return false;
        }
        leaveUserAdd.setLeaveId(add.getLeaveId());
        return iLeaveUserService.save(leaveUserAdd);
    }



    @Override
    public Boolean updateByEditBo(LeaveManageEditBo bo) {
        LeaveManage update = BeanUtil.toBean(bo, LeaveManage.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(LeaveManage entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean updateByLeaveRevoke(Integer leaveId) {
        LeaveManage leaveManage=this.getById(leaveId);
        leaveManage.setLeaveRevoke(1);
        return this.saveOrUpdate(leaveManage);
    }

    @Override
    public Boolean updateByCancellationStatus(Integer leaveUserId) {
        LeaveUser leaveUser=iLeaveUserService.getById(leaveUserId);
        leaveUser.setCancellationStatus(1l);

        return iLeaveUserService.saveOrUpdate(leaveUser);
    }
}
